﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using S9Engine;


public partial class Gallery : Parser
{
    private long _id;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            InitAll();

            Session["CurrentPage"] = "Gallery.aspx";

            if (!CheckLogin()) Response.Redirect("login.aspx");

            _parser.SetCurrentDir("~/html");
            _parser.SetLanguageFile("~/templates/language.txt", _currentLanguage);

            _id = Request["id"] == null ? 0 : long.Parse(Request["id"].ToString() ?? "0");

            string type = Request["type"] ?? "default";

            _DR.Refresh(System.Data.Objects.RefreshMode.StoreWins, _DR.Images);
            _DR.Refresh(System.Data.Objects.RefreshMode.StoreWins, _DR.Terms);
            _DR.Refresh(System.Data.Objects.RefreshMode.StoreWins, _DR.Taxonomy);

            LoadFile("gallery.html", false);
            LoadGallery();
        }
        catch (Exception ex)
        {
            WriteError(ex);
        }
        finally
        {
            _parser.Parse("Main");
            _parser.Out("Main");
        }
    }

    private void LoadGallery()
    {
        _parser.Assign("albumID", _id.ToString());

        List<Images> images = _DR.Images.Join(_DR.Relations.Where(r => r.Type.Equals("album-image") && r.Parent.Equals(_id)), i => i.ID, r => r.Child, (i, r) => i).ToList();

        double totalRecord = images.Count();
        int page = int.Parse(Request["page"] ?? "1");
        int pageSize = _rowsPerPage;
        int totalPage = (int)(totalRecord / pageSize);

        if (totalPage * pageSize < totalRecord) totalPage++;

        if (totalRecord != 0)
        {
            int startRecord = (page == 1 ? 0 : ((page - 1) * pageSize));
            int endRecord = (int)(page * pageSize > totalRecord ? totalRecord : page * pageSize);

            for (int i = startRecord; i < endRecord; i++)
            {
                _parser.Assign("imageID", images[i].ID.ToString());
                _parser.Assign("imageURL", images[i].URL.Replace(_webcmsPath, "~/.."));
                _parser.Assign("imageDescription", images[i].Description);
                _parser.Parse("Main.Content.EditForm.Gallery.GalleryItem");
            }

            _parser.Assign("totalPage", totalPage.ToString());
            _parser.Assign("totalRecord", totalRecord.ToString());
            _parser.Assign("currentPage", page.ToString());

            if (totalPage != 1)
            {
                int pagingPage = page / _pageRange;
                int startPage = 1;
                int endPage = 1;

                if (page % _pageRange == (_pageRange - 1) && page > _pageRange)
                {
                    startPage = page;
                }
                else
                {
                    startPage = (pagingPage * _pageRange == 0 ? 1 : pagingPage * _pageRange);
                }

                endPage = (startPage + (_pageRange - 1) > totalPage ? totalPage : startPage + (_pageRange - 1));

                _parser.Assign("pageIndexPrevious", (page == 1 ? 1 : page - 1).ToString());
                _parser.Assign("pageIndexNext", (page == totalPage ? totalPage : page + 1).ToString());


                for (int i = startPage; i <= endPage; i++)
                {
                    _parser.Assign("currentPageStyle", page == i ? " class=\"current\"" : string.Empty);
                    _parser.Assign("pageIndex", i.ToString());
                    _parser.Parse("Main.Content.EditForm.Paging.PagingItem");
                }

                _parser.Parse("Main.Content.EditForm.Paging.PagingPrevious");
                _parser.Parse("Main.Content.EditForm.Paging.PagingNext");
            }

            _parser.Parse("Main.Content.EditForm.Gallery");
            _parser.Parse("Main.Content.EditForm.Paging");
        }

        _parser.Parse("Main.Content.EditForm.AddAndEdit");
        _parser.Parse("Main.Content.EditForm");
        _parser.Parse("Main.Content");
    }
}